--[[
    OneCompanion, a cool companion bag for World of Warcraft
    Copyright (C) 2010 Arnaud Dovi (ad@heapoverflow.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
]]

--[[
$Rev:: 0                     $:  Revision of last commit
$Author:: arnaud             $:  Author of last commit
$Date:: 2012-09-02 22:37:24 #$:  Date of last commit
$WoW:: 5.0.5.16030           $:  Client of the last tests
]]

if not (select(4,GetBuildInfo()) >= 30000) then return end
local MINOR_VERSION = tonumber(("$Revision: 44 $"):match("%d+"))
if MINOR_VERSION > _G.OneCompanion_MINOR_VERSION then _G.OneCompanion_MINOR_VERSION = MINOR_VERSION end

OneCompanionOptions = {}
local L = LibStub("AceLocale-3.0"):GetLocale("OneCompanion")
local AC = LibStub("AceConfig-3.0")
local ACD = LibStub("AceConfigDialog-3.0")
local stratas = {["BACKGROUND"] = "BACKGROUND", ["LOW"] = "LOW", ["MEDIUM"] = "MEDIUM", ["HIGH"] = "HIGH", ["DIALOG"] = "DIALOG",
                 ["FULLSCREEN"] = "FULLSCREEN", ["FULLSCREEN_DIALOG"] = "FULLSCREEN_DIALOG", ["TOOLTIP"] = "TOOLTIP"}
local borderstyles = {
    [""] = "none",
    ["Interface\\Tooltips\\UI-Tooltip-Border"] = "UI-Tooltip-Border",
    ["Interface\\AchievementFrame\\UI-Achievement-WoodBorder"] = "UI-Achievement-WoodBorder",
    ["Interface\\DialogFrame\\UI-DialogBox-Border"] = "UI-DialogBox-Border",
    ["Interface\\DialogFrame\\UI-DialogBox-Gold-Border"] = "UI-DialogBox-Gold-Border",
    ["Interface\\CHARACTERFRAME\\UI-Party-Border"] = "UI-Party-Border",
    ["Interface\\BUTTONS\\UI-SliderBar-Border"] = "UI-SliderBar-Border",
    ["Interface\\MINIMAP\\TooltipBackdrop"] = "TooltipBackdrop",
    ["Interface\\QuestFrame\\UI-TextBackground-Backdrop"] = "UI-TextBackground-Backdrop",
    ["Interface\\Tooltips\\ChatBubble-Backdrop"] = "ChatBubble-Backdrop",
}
local bgstyles = {
    [""] = "none",
    ["World\\Expansion01\\Doodads\\HellfirePeninsula\\DARKPORTAL\\Hellfire_DarkPortal_Background"] = "Hellfire_DarkPortal_Background",
    ["World\\ENVIRONMENT\\DOODAD\\HELLFIREPENINSULA\\DARKPORTALFX\\HELLFIRE_DARKPORTAL_BACKGROUND_CLOUDS"] = "HELLFIRE_DARKPORTAL_BACKGROUND_CLOUDS",
    ["Interface\\AchievementFrame\\UI-Achievement-AchievementBackground"] = "UI-Achievement-AchievementBackground",
    ["Interface\\CHARACTERFRAME\\UI-Party-Background"] = "UI-Party-Background",
    ["Interface\\ChatFrame\\ChatFrameBackground"] = "ChatFrameBackground",
    ["Interface\\DialogFrame\\UI-DialogBox-Background"] = "UI-DialogBox-Background",
    ["Interface\\DialogFrame\\UI-DialogBox-Gold-Background"] = "UI-DialogBox-Gold-Background",
    ["Interface\\DressUpFrame\\DressUpBackground-BloodElf1"] = "DressUpBackground-BloodElf1",
    ["Interface\\DressUpFrame\\DressUpBackground-Draenei1"] = "DressUpBackground-Draenei1",
    ["Interface\\DressUpFrame\\DressUpBackground-Dwarf1"] = "DressUpBackground-Dwarf1",
    ["Interface\\DressUpFrame\\DressUpBackground-Human1"] = "DressUpBackground-Human1",
    ["Interface\\DressUpFrame\\DressUpBackground-NightElf1"] = "DressUpBackground-NightElf1",
    ["Interface\\DressUpFrame\\DressUpBackground-Orc1"] = "DressUpBackground-Orc1",
    ["Interface\\DressUpFrame\\DressUpBackground-Scourge1"] = "DressUpBackground-Scourge1",
    ["Interface\\DressUpFrame\\DressUpBackground-Tauren1"] = "DressUpBackground-Tauren1",
    ["Interface\\TabardFrame\\TabardFrameBackground"] = "TabardFrameBackground",
    ["Interface\\TARGETINGFRAME\\UI-TargetingFrame-LevelBackground"] = "UI-TargetingFrame-LevelBackground",
    ["Interface\\Tooltips\\UI-Tooltip-Background"] = "UI-Tooltip-Background",
    ["Textures\\GuildEmblems\\Background_00_TL_U"] = "Background_00_TL_U",
    ["Textures\\GuildEmblems\\Background_03_TL_U"] = "Background_03_TL_U",
    ["Textures\\GuildEmblems\\Background_06_TL_U"] = "Background_06_TL_U",
    ["Textures\\GuildEmblems\\Background_09_TL_U"] = "Background_09_TL_U",
    ["Textures\\GuildEmblems\\Background_12_TL_U"] = "Background_12_TL_U",
    ["Textures\\GuildEmblems\\Background_15_TL_U"] = "Background_15_TL_U",
    ["Textures\\GuildEmblems\\Background_18_TL_U"] = "Background_18_TL_U",
    ["Textures\\GuildEmblems\\Background_21_TL_U"] = "Background_21_TL_U",
    ["Textures\\GuildEmblems\\Background_24_TL_U"] = "Background_24_TL_U",
    ["Textures\\GuildEmblems\\Background_27_TL_U"] = "Background_27_TL_U",
    ["Textures\\GuildEmblems\\Background_30_TL_U"] = "Background_30_TL_U",
    ["Textures\\GuildEmblems\\Background_33_TL_U"] = "Background_33_TL_U",
    ["Textures\\GuildEmblems\\Background_36_TL_U"] = "Background_36_TL_U",
    ["Textures\\GuildEmblems\\Background_39_TL_U"] = "Background_39_TL_U",
    ["Textures\\GuildEmblems\\Background_42_TL_U"] = "Background_42_TL_U",
    ["Textures\\GuildEmblems\\Background_45_TL_U"] = "Background_45_TL_U",
    ["Textures\\GuildEmblems\\Background_48_TL_U"] = "Background_48_TL_U",
    ["Interface\\Glues\\CharacterCreate\\UI-CharacterCreate-Background"] = "UI-CharacterCreate-Background",
}

local defaults = {
    profile = {
        sizes = {
            pt = "CENTER",
            rpt = "CENTER",
            breannipt = "CENTER",
            breannirpt = "CENTER",
            x = 0,
            y = 0,
            w = 225,
            h = 200,
            mw = 225,
            mh = 200,
            sc = 1,
            bsc = 1,
            breannix = 0,
            breanniy = 0,
            buttonpad = 5,
            borderpad = 5,
        },
        colors = {
            index = {0.203921568627451, 0.2117647058823529, 0.2078431372549019, 0.9299999997019768},
            footer = {0.1215686274509804, 0.1254901960784314, 0.1254901960784314, 1},
            bindex = {0.7019607843137254, 0.3137254901960784, 0.5686274509803921, 1},
            bfooter = {0.807843137254902, 0.1254901960784314, 0.1176470588235294, 1},
            model = {0.203921568627451, 0.2117647058823529, 0.2078431372549019, 0.9299999997019768},
            bmodel = {0.7019607843137254, 0.3137254901960784, 0.5686274509803921, 1},
        },
        styles = {
            background_index = "Interface\\ChatFrame\\ChatFrameBackground",
            border_index = "Interface\\Tooltips\\UI-Tooltip-Border",
            tile_index = false,
            tileSize_index = 16,
            edgeSize_index = 16,
            i_left_index = 5,
            i_right_index = 5,
            i_top_index = 5,
            i_bottom_index = 5,
            background_footer = "Interface\\ChatFrame\\ChatFrameBackground",
            border_footer = "Interface\\Tooltips\\UI-Tooltip-Border",
            tile_footer = false,
            tileSize_footer = 16,
            edgeSize_footer = 16,
            i_left_footer = 5,
            i_right_footer = 5,
            i_top_footer = 5,
            i_bottom_footer = 5,
            background_model = "Interface\\ChatFrame\\ChatFrameBackground",
            border_model = "Interface\\Tooltips\\UI-Tooltip-Border",
            tile_model = false,
            tileSize_model = 16,
            edgeSize_model = 16,
            i_left_model = 5,
            i_right_model = 5,
            i_top_model = 5,
            i_bottom_model = 5,
        },
        misc = {
            clamp = true,
            mclamp = false,
            grip = false,
            mgrip = false,
            tip = false,
            lockbreanni = false,
            lockbag = false,
            stratabase = "BACKGROUND",
            stratabtn = "BACKGROUND",
        }
    }
}

local options = {
    type = "group",
    name = "OneCompanion",
    desc = "OneCompanion options",
    childGroups = "tab",
    handler = OneCompanionOptions,
    args = {
--[[		rockConfig = {
            order = 0,
            type = "execute",
            name = L["RockConfig"],
            desc = L["Open the Rock config window"],
            func = function()
                OneCompanion:OpenConfigMenu("OneCompanion")
                ACD:CloseAll()
                InterfaceOptionsFrame:Hide()
            end
        },]]
        aceConfig = {
            order = 5,
            type = "execute",
            name = L["AceGUI"],
            desc = L["Open the Ace3 config window"],
            func = function()
                ACD:SetDefaultSize("OneCompanion", 600, 550)
                ACD:Open("OneCompanion")
--				if getglobal("LibRockConfig-1.0_Frame") then getglobal("LibRockConfig-1.0_Frame"):Hide() end
            end
        },
        blizzConfig = {
            order = 10,
            type = "execute",
            name = L["BlizzardUI"],
            desc = L["Open the Blizzard config window"],
            func = function()
                InterfaceOptionsFrame:SetWidth(850)
                InterfaceOptionsFrame_OpenToCategory(OneCompanion.optionsFrame)
--				if getglobal("LibRockConfig-1.0_Frame") then getglobal("LibRockConfig-1.0_Frame"):Hide() end
--				ACD:CloseAll()
            end
        },
        display = {
            type = "group",
            name = L["Display"],
            desc = L["Display options"],
            childGroups = "tab",
            args = {
                sizes = {
                    order = 0,
                    type = "group",
                    name = L["Sizes"],
                    desc = L["Sizes options"],
                    args = {
                        mainframe = {
                            order = 0,
                            type = "header",
                            name = L["Main frame"],
                        },
                        h = {
                            order = 1,
                            name = L["Height"],
                            desc = L["Change the height"],
                            type = "range",
                            min = 68, max = 1000, step = 1,
                            set = function(_, value) OneCompanion.db.profile.sizes.h = value OneCompanionFrames.base:SetHeight(OneCompanion.db.profile.sizes.h) OneCompanionFrames.ShowSlider() end,
                            get = function() return OneCompanionFrames.base:GetHeight() end,
                        },
                        w = {
                            order = 3,
                            name = L["Width"],
                            desc = L["Change the width"],
                            type = "range",
                            min = 59, max = 1000, step = 1,
                            set = "ToggleMainFrameWidth",
                            get = function() return OneCompanionFrames.base:GetWidth() end,
                        },
                        modelframe = {
                            order = 4,
                            type = "header",
                            name = L["Model frame"],
                        },
                        mh = {
                            order = 6,
                            name = L["Height"],
                            desc = L["Change the height"],
                            type = "range",
                            min = 40, max = 1000, step = 1,
                            set = function(_, value) OneCompanion.db.profile.sizes.mh = value OneCompanionFrames.model:SetHeight(OneCompanion.db.profile.sizes.mh) end,
                            get = function() return OneCompanionFrames.model:GetHeight() end,
                        },
                        mw = {
                            order = 7,
                            name = L["Width"],
                            desc = L["Change the width"],
                            type = "range",
                            min = 40, max = 1000, step = 1,
                            set = "ToggleModelFrameWidth",
                            get = function() return OneCompanionFrames.model:GetWidth() end,
                        },
                        buttonframe = {
                            order = 8,
                            type = "header",
                            name = L["Button frames"],
                        },
                        sc = {
                            order = 10,
                            name = L["Scale"],
                            desc = L["Change the scale"],
                            type = "range",
                            min = .1, max = 2, step = 0.05,
                            set = "ToggleButtonScale",
                            get = function() return OneCompanion.db.profile.sizes.sc end,
                        },
                        bsc = {
                            order = 15,
                            name = L["Breanni scale"],
                            desc = L["Change the Breanni's button scale"],
                            type = "range",
                            min = .1, max = 2, step = 0.05,
                            set = "ToggleBreanniScale",
                            get = function() return OneCompanion.db.profile.sizes.bsc end,
                        },
                        buttonpad = {
                            order = 11,
                            name = L["Padding"],
                            desc = L["Change the padding"],
                            type = "range",
                            min = 0, max = 20, step = 0.1,
                            set = function(_, value) OneCompanion.db.profile.sizes.buttonpad = value OneCompanionOptions:ToggleButtonScale(nil, nil, value, nil) end,
                            get = function() return OneCompanion.db.profile.sizes.buttonpad end,
                        },
--[[						borderpad = {
                            order = 15,
                            name = L["Border padding"],
                            desc = L["Change the size between each border's button and the border"],
                            type = "range",
                            min = 5, max = 20, step = 1,
                            set = "ToggleBorderPadding",
                            get = "IsBorderPadding",
                        },]]
                    }
                },
                colors = {
                    order = 5,
                    type = "group",
                    name = L["Colors"],
                    desc = L["Colors options"],
                    args = {
                        mainframe = {
                            order = 0,
                            type = "header",
                            name = L["Main frame"],
                        },
                        index = {
                            order = 1,
                            name = L["Background"],
                            desc = L["Change the background color"],
                            type = "color",
                            hasAlpha = true,
                            get = function() return unpack(OneCompanion.db.profile.colors.index) end,
                            set = "SetColor",
                        },
                        bindex = {
                            order = 5,
                            name = L["Border"],
                            desc = L["Change the border color"],
                            type = "color",
                            hasAlpha = true,
                            get = function() return unpack(OneCompanion.db.profile.colors.bindex) end,
                            set = "SetColor",
                        },
                        footerframe = {
                            order = 9,
                            type = "header",
                            name = L["Footer frame"],
                        },
                        footer = {
                            order = 10,
                            name = L["Background"],
                            desc = L["Change the background color"],
                            type = "color",
                            hasAlpha = true,
                            get = function() return unpack(OneCompanion.db.profile.colors.footer) end,
                            set = "SetColor",
                        },
                        bfooter = {
                            order = 15,
                            name = L["Border"],
                            desc = L["Change the border color"],
                            type = "color",
                            hasAlpha = true,
                            get = function() return unpack(OneCompanion.db.profile.colors.bfooter) end,
                            set = "SetColor",
                        },
                        modelframe = {
                            order = 16,
                            type = "header",
                            name = L["Model frame"],
                        },
                        model = {
                            order = 20,
                            name = L["Background"],
                            desc = L["Change the background color"],
                            type = "color",
                            hasAlpha = true,
                            get = function() return unpack(OneCompanion.db.profile.colors.model) end,
                            set = "SetColor",
                        },
                        bmodel = {
                            order = 25,
                            name = L["Border"],
                            desc = L["Change the border color"],
                            type = "color",
                            hasAlpha = true,
                            get = function() return unpack(OneCompanion.db.profile.colors.bmodel) end,
                            set = "SetColor",
                        },
                    }
                },
                styles = {
                    order = 6,
                    type = "group",
                    name = L["Styles"],
                    desc = L["Styles options"],
                    args = {
                        mainframe = {
                            order = 0,
                            type = "header",
                            name = L["Main frame"],
                        },
                        background_index = {
                            order = 1,
                            type = "select",
                            name = L["Background style"],
                            desc = L["Change the background style"],
                            values = bgstyles,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.background_index end,
                        },
                        border_index = {
                            order = 5,
                            type = "select",
                            name = L["Border style"],
                            desc = L["Change the border style"],
                            values = borderstyles,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.border_index end,
                        },
                        tile_index = {
                            order = 10,
                            type = "toggle",
                            name = L["tile"],
                            desc = L["Change the background tile"],
                            set = function(value) OneCompanionOptions:ToggleBackdrop("index", value) end,
                            get = function() return OneCompanion.db.profile.styles.tile_index end,
                        },
                        tileSize_index = {
                            order = 15,
                            type = "range",
                            name = L["tileSize"],
                            desc = L["Change the background tileSize"],
                            min = 0, max = 500, step = 0.1,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.tileSize_index end,
                        },
                        edgeSize_index = {
                            order = 20,
                            type = "range",
                            name = L["edgeSize"],
                            desc = L["Change the background edgeSize"],
                            min = 0, max = 500, step = 0.1,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.edgeSize_index end,
                        },
                        i_left_index = {
                            order = 25,
                            type = "range",
                            name = L["insetLeft"],
                            desc = L["Change the insetLeft value"],
                            min = 0, max = 50, step = 0.1,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.i_left_index end,
                        },
                        i_right_index = {
                            order = 30,
                            type = "range",
                            name = L["insetRight"],
                            desc = L["Change the insetRight value"],
                            min = 0, max = 50, step = 0.1,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.i_right_index end,
                        },
                        i_top_index = {
                            order = 35,
                            type = "range",
                            name = L["insetTop"],
                            desc = L["Change the insetTop value"],
                            min = 0, max = 50, step = 0.1,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.i_top_index end,
                        },
                        i_bottom_index = {
                            order = 40,
                            type = "range",
                            name = L["insetBottom"],
                            desc = L["Change the insetBottom value"],
                            min = 0, max = 50, step = 0.1,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.i_bottom_index end,
                        },
                        footerframe = {
                            order = 45,
                            type = "header",
                            name = L["Footer frame"],
                        },
                        background_footer = {
                            order = 50,
                            type = "select",
                            name = L["Background style"],
                            desc = L["Change the background style"],
                            values = bgstyles,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.background_footer end,
                        },
                        border_footer = {
                            order = 55,
                            type = "select",
                            name = L["Border style"],
                            desc = L["Change the border style"],
                            values = borderstyles,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.border_footer end,
                        },
                        tile_footer = {
                            order = 60,
                            type = "toggle",
                            name = L["tile"],
                            desc = L["Change the background tile"],
                            set = function(value) OneCompanionOptions:ToggleBackdrop("footer", value) end,
                            get = function() return OneCompanion.db.profile.styles.tile_footer end,
                        },
                        tileSize_footer = {
                            order = 65,
                            type = "range",
                            name = L["tileSize"],
                            desc = L["Change the background tileSize"],
                            min = 0, max = 500, step = 0.1,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.tileSize_footer end,
                        },
                        edgeSize_footer = {
                            order = 70,
                            type = "range",
                            name = L["edgeSize"],
                            desc = L["Change the background edgeSize"],
                            min = 0, max = 500, step = 0.1,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.edgeSize_footer end,
                        },
                        i_left_footer = {
                            order = 75,
                            type = "range",
                            name = L["insetLeft"],
                            desc = L["Change the insetLeft value"],
                            min = 0, max = 50, step = 0.1,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.i_left_footer end,
                        },
                        i_right_footer = {
                            order = 80,
                            type = "range",
                            name = L["insetRight"],
                            desc = L["Change the insetRight value"],
                            min = 0, max = 50, step = 0.1,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.i_right_footer end,
                        },
                        i_top_footer = {
                            order = 85,
                            type = "range",
                            name = L["insetTop"],
                            desc = L["Change the insetTop value"],
                            min = 0, max = 50, step = 0.1,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.i_top_footer end,
                        },
                        i_bottom_footer = {
                            order = 90,
                            type = "range",
                            name = L["insetBottom"],
                            desc = L["Change the insetBottom value"],
                            min = 0, max = 50, step = 0.1,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.i_bottom_footer end,
                        },
                        modelframe = {
                            order = 95,
                            type = "header",
                            name = L["Model frame"],
                        },
                        background_model = {
                            order = 100,
                            type = "select",
                            name = L["Background style"],
                            desc = L["Change the background style"],
                            values = bgstyles,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.background_model end,
                        },
                        border_model = {
                            order = 105,
                            type = "select",
                            name = L["Border style"],
                            desc = L["Change the border style"],
                            values = borderstyles,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.border_model end,
                        },
                        tile_model = {
                            order = 110,
                            type = "toggle",
                            name = L["tile"],
                            desc = L["Change the background tile"],
                            set = function(value) OneCompanionOptions:ToggleBackdrop("model", value) end,
                            get = function() return OneCompanion.db.profile.styles.tile_model end,
                        },
                        tileSize_model = {
                            order = 115,
                            type = "range",
                            name = L["tileSize"],
                            desc = L["Change the background tileSize"],
                            min = 0, max = 500, step = 0.1,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.tileSize_model end,
                        },
                        edgeSize_model = {
                            order = 120,
                            type = "range",
                            name = L["edgeSize"],
                            desc = L["Change the background edgeSize"],
                            min = 0, max = 500, step = 0.1,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.edgeSize_model end,
                        },
                        i_left_model = {
                            order = 125,
                            type = "range",
                            name = L["insetLeft"],
                            desc = L["Change the insetLeft value"],
                            min = 0, max = 50, step = 0.1,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.i_left_model end,
                        },
                        i_right_model = {
                            order = 130,
                            type = "range",
                            name = L["insetRight"],
                            desc = L["Change the insetRight value"],
                            min = 0, max = 50, step = 0.1,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.i_right_model end,
                        },
                        i_top_model = {
                            order = 135,
                            type = "range",
                            name = L["insetTop"],
                            desc = L["Change the insetTop value"],
                            min = 0, max = 50, step = 0.1,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.i_top_model end,
                        },
                        i_bottom_model = {
                            order = 140,
                            type = "range",
                            name = L["insetBottom"],
                            desc = L["Change the insetBottom value"],
                            min = 0, max = 50, step = 0.1,
                            set = "ToggleBackdrop",
                            get = function() return OneCompanion.db.profile.styles.i_bottom_model end,
                        },
                    }
                },
                misc = {
                    order = 10,
                    type = "group",
                    name = L["Misc"],
                    desc = L["Miscellaneous options"],
                    args = {
                        mainframe = {
                            order = 0,
                            type = "header",
                            name = L["Main frame"],
                        },
                        clamp = {
                            order = 1,
                            type = "toggle",
                            name = L["Clamp"],
                            desc = L["Clamp to the screen"],
                            set = "ToggleMainFrameClamp",
                            get = function() return OneCompanion.db.profile.misc.clamp end,
                        },
                        grip = {
                            order = 5,
                            type = "toggle",
                            name = L["Hide grip"],
                            desc = L["Hide the grip"],
                            set = "ToggleShowGrip",
                            get = function() return OneCompanion.db.profile.misc.grip end,
                        },
                        modelframe = {
                            order = 10,
                            type = "header",
                            name = L["Model frame"],
                        },
                        mclamp = {
                            order = 11,
                            type = "toggle",
                            name = L["Clamp"],
                            desc = L["Clamp to the screen"],
                            set = "ToggleModelFrameClamp",
                            get = function() return OneCompanion.db.profile.misc.mclamp end,
                        },
                        mgrip = {
                            order = 16,
                            type = "toggle",
                            name = L["Hide grip"],
                            desc = L["Hide the grip"],
                            set = "ToggleShowModelGrip",
                            get = function() return OneCompanion.db.profile.misc.mgrip end,
                        },
                        tipsframe = {
                            order = 20,
                            type = "header",
                            name = L["Tooltips frames"],
                        },
                        tip = {
                            order = 25,
                            type = "toggle",
                            name = L["Hide"],
                            desc = L["Hide or show the tooltips shown on mouseovering AddOn icons"],
                            set = "ToggleShowTooltips",
                            get = function() return OneCompanion.db.profile.misc.tip end,
                        },
                        locks = {
                            order = 30,
                            type = "header",
                            name = L["Lock frames"],
                        },
                        lockbreanni = {
                            order = 35,
                            type = "toggle",
                            name = L["Lock Breanni"],
                            desc = L["Lock the position of Breanni's icon"],
                            set = "ToggleLockBreanni",
                            get = function() return OneCompanion.db.profile.misc.lockbreanni end,
                        },
                        lockbag = {
                            order = 40,
                            type = "toggle",
                            name = L["Lock inventory"],
                            desc = L["Lock the position of the companion's inventory bag"],
                            set = "ToggleLockBag",
                            get = function() return OneCompanion.db.profile.misc.lockbag end,
                        },
                        stratas = {
                            order = 45,
                            type = "header",
                            name = L["Stratas"],
                        },
                        stratabase = {
                            order = 50,
                            type = "select",
                            name = L["Base strata"],
                            desc = L["Change the base frame strata"],
                            values = stratas,
                            set = "ToggleStrata",
                            get = function() return OneCompanion.db.profile.misc.stratabase end,
                        },
                        stratabtn = {
                            order = 55,
                            type = "select",
                            name = L["Button strata"],
                            desc = L["Change the button frames strata"],
                            values = stratas,
                            set = "ToggleStrata",
                            get = function() return OneCompanion.db.profile.misc.stratabtn end,
                        },
                    },
                },
            }
        },
        options = {
            type = "group",
            name = L["Options"],
            desc = L["AddOn options"],
            args = {}
        },
    }
}

OneCompanionOptions.options = options
OneCompanionOptions.defaults = defaults

function OneCompanionOptions:ToggleBackdrop(key, value)
    local self, fra, opt, ocdb = OneCompanion, OneCompanionFrames, OneCompanionOptions, OneCompanion.db.profile.styles
    if type(key) == "table" then ocdb[key[3]] = value
    elseif key == "index" then ocdb.tile_index = not ocdb.tile_index
    elseif key == "footer" then ocdb.tile_footer = not ocdb.tile_footer
    elseif key == "model" then ocdb.tile_model = not ocdb.tile_model end
    local Backdrop_index = {
        bgFile = ocdb.background_index,
        edgeFile = ocdb.border_index,
        tile = ocdb.tile_index, tileSize = ocdb.tileSize_index, edgeSize = ocdb.edgeSize_index,
        insets = { left	= ocdb.i_left_index, right = ocdb.i_right_index, top = ocdb.i_top_index, bottom = ocdb.i_bottom_index }
        }
    local Backdrop_footer = {
        bgFile = ocdb.background_footer,
        edgeFile = ocdb.border_footer,
        tile = ocdb.tile_footer, tileSize = ocdb.tileSize_footer, edgeSize = ocdb.edgeSize_footer,
        insets = { left	= ocdb.i_left_footer, right = ocdb.i_right_footer, top = ocdb.i_top_footer, bottom = ocdb.i_bottom_footer }
        }
    local Backdrop_model = {
        bgFile = ocdb.background_model,
        edgeFile = ocdb.border_model,
        tile = ocdb.tile_model, tileSize = ocdb.tileSize_model, edgeSize = ocdb.edgeSize_model,
        insets = { left	= ocdb.i_left_model, right = ocdb.i_right_model, top = ocdb.i_top_model, bottom = ocdb.i_bottom_model }
        }
    fra.model:SetBackdrop(Backdrop_model)
    fra.footer:SetBackdrop(Backdrop_footer)
    fra.index:SetBackdrop(Backdrop_index)
    opt:SetColor(nil, nil, nil, nil, nil)
end

function OneCompanionOptions:ToggleStrata(key, value)
    local self, fra, btn, ocdb = OneCompanion, OneCompanionFrames, OneCompanionButtons, OneCompanion.db.profile.misc
    if key then ocdb[key[3]] = value end
    fra.base:SetFrameStrata(ocdb.stratabase)
    for i, j in ipairs(btn) do
        j:SetToplevel(true)
        j:SetFrameStrata(ocdb.stratabtn)
    end
end

function OneCompanionOptions:ToggleLockBag(value)
    local self, fra, btn, ocdb = OneCompanion, OneCompanionFrames, OneCompanionButtons, OneCompanion.db.profile.misc
    if value then ocdb.lockbag = not ocdb.lockbag end
    if ocdb.lockbag then
        fra.base:SetMovable(false)
        OCstart, OCstop = nil, nil
    else
        fra.base:SetMovable(true)
        OCstart = function() fra.base:StartMoving() end
        OCstop = function() fra.base:StopMovingOrSizing() fra:SetAnchors(fra.base) end
    end
    for i, j in pairs(fra) do
        if type(fra[i]) ~= "function" and i ~= "breanni" and i ~= "grip" and i ~= "mgrip" and i ~= "slider" and i ~= "footerText" and i ~= "footerRev" and i ~= "modelindex" then
            if i == "scrollindex" then fra[i]:SetFrameLevel(0) end
            fra[i]:SetScript("OnMouseDown", OCstart)
            fra[i]:SetScript("OnMouseUp", OCstop)
        end
        if i == "modelindex" then
            fra[i]:SetScript("OnMouseDown", function() fra[i].startx, fra[i].starty = GetCursorPosition() fra[i]:SetScript("OnUpdate", fra.OnRotateModel) end)
            fra[i]:SetScript("OnMouseUp", function() fra[i]:SetScript("OnUpdate", nil) end)
        end
    end
    for i, j in ipairs(btn) do
        j:SetScript("OnDragStart", function() PickupCompanion(j.type,j.petID) end)
    end
end

function OneCompanionOptions:ToggleLockBreanni(value)
    local self, fra, ocdb = OneCompanion, OneCompanionFrames, OneCompanion.db.profile.misc
    if value then ocdb.lockbreanni = not ocdb.lockbreanni end
    if ocdb.lockbreanni then
        fra.breanni:SetMovable(false)
        fra.breanni:SetScript("OnDragStart", nil)
        fra.breanni:SetScript("OnDragStop", nil)
    else
        fra.breanni:SetMovable(true)
        fra.breanni:SetScript("OnDragStart", function() fra.breanni:StartMoving() end)
        fra.breanni:SetScript("OnDragStop", function() fra.breanni:StopMovingOrSizing() fra:SetBreanniAnchors(fra.breanni) end)
    end
end

--[[function OneCompanionOptions:ToggleBorderPadding(_, value)
    local self, opt = OneCompanion, OneCompanionOptions
    if value then self.db.profile.sizes.borderpad = value end
    opt:ToggleButtonScale(nil, nil, nil, value)
end

function OneCompanionOptions:IsBorderPadding()
    local self = OneCompanion
    return self.db.profile.sizes.borderpad
end]]

function OneCompanionOptions:ToggleMainFrameWidth(_, value)
    local self, fra, opt, ocdb = OneCompanion, OneCompanionFrames, OneCompanionOptions, OneCompanion.db.profile.sizes
    if value then ocdb.w = value end
    fra.base:SetWidth(ocdb.w)
    fra.index:SetWidth(ocdb.w)
    fra.footer:SetWidth(ocdb.w)
    opt:ToggleButtonScale(nil, nil, nil, nil)
end

function OneCompanionOptions:ToggleModelFrameWidth(_, value)
    local self, fra, ocdb = OneCompanion, OneCompanionFrames, OneCompanion.db.profile.sizes
    if value then ocdb.mw = value end
    fra.model:SetWidth(ocdb.mw)
end

function OneCompanionOptions:IsMainFrameHeight()
    local fra = OneCompanionFrames
    local value = fra.base:GetHeight()
    return value
end

function OneCompanionOptions:ToggleBreanniScale(_, value)
    local self, fra, ocdb = OneCompanion, OneCompanionFrames, OneCompanion.db.profile.sizes
    if value then ocdb.bsc = value end
    fra.breanni:SetScale(ocdb.bsc)
    fra.breanni:SetPoint(ocdb.breannipt, UIParent, ocdb.breannirpt, ocdb.breannix / ocdb.bsc, ocdb.breanniy / ocdb.bsc)
end

function OneCompanionOptions:ToggleButtonScale(_, v, w, x)
    local self, fra, btn, starter, l, ocdb = OneCompanion, OneCompanionFrames, OneCompanionButtons, 1, 0, OneCompanion.db.profile.sizes
    if v then ocdb.sc = v else v = ocdb.sc end
    if w then ocdb.buttonpad = w else w = ocdb.buttonpad end
    if x then ocdb.borderpad = x else x = ocdb.borderpad end
    for i, j in ipairs(btn) do
        local function newLine()
            local h, a = j:GetHeight(), j:GetHeight()
            j:SetPoint("TOPLEFT", fra.child, "TOPLEFT", x, - x - (h + w / v) * l)
            OneCompanionMaxHeight = ((l + 1) * a * v ) + (2 * x) + (w * l)
        end
        j:ClearAllPoints()
        j:SetScale(v)
        if i == 1 then
            newLine()
        else
            local f, g = j:GetWidth(), fra.index:GetWidth()
            if g - 10 > ((i - starter + 1) * f * v) + ((i - starter + 1) * w) then
                j:SetPoint("LEFT", btn[i-1], "RIGHT", w / v, 0)
            else
                l = l + 1
                starter = i
                newLine()
            end
        end
    end
    local f, s, p = fra.footerText:GetFont()
    local e = fra.footer:GetWidth()
    if e < 122 then
        fra.footerText:SetFont(f, 6, p)
        fra.footerRev:SetFont(f, 6, p)
        fra.footerText:SetPoint("BOTTOMRIGHT", fra.footerRev, "TOPRIGHT", 0, 0)
        fra.footerText:SetText("One|cffffcc00C|r")
    elseif e < 173 then
        fra.footerText:SetFont(f, 8, p)
        fra.footerRev:SetFont(f, 8, p)
        fra.footerText:SetPoint("BOTTOMRIGHT", fra.footerRev, "TOPRIGHT", 0, 0)
        fra.footerText:SetText("One|cffffcc00Companion|r")
    else
        fra.footerText:SetFont(f, 12, p)
        fra.footerRev:SetFont(f, 8, p)
        fra.footerText:SetPoint("BOTTOMRIGHT", fra.footerRev, "TOPLEFT", 0, -4)
        fra.footerText:SetText("One|cffffcc00Companion|r")
    end
    fra.ShowSlider()
end

function OneCompanionOptions:ToggleMainFrameClamp(value)
    local self, fra, ocdb = OneCompanion, OneCompanionFrames, OneCompanion.db.profile.misc
    if value then ocdb.clamp = not ocdb.clamp end
    fra.base:SetClampedToScreen(ocdb.clamp)
end

function OneCompanionOptions:ToggleModelFrameClamp(value)
    local self, fra, ocdb = OneCompanion, OneCompanionFrames, OneCompanion.db.profile.misc
    if value then ocdb.mclamp = not ocdb.mclamp end
    fra.model:SetClampedToScreen(ocdb.mclamp)
end

function OneCompanionOptions:SetColor(key, r, g, b, a)
    local self, fra, ocdb = OneCompanion, OneCompanionFrames, OneCompanion.db.profile.colors
    if key then
        local t = ocdb[key[3]]
        t[1] = r t[2] = g t[3] = b t[4] = a
    end
    r, g, b, a = unpack(ocdb.index) fra.index:SetBackdropColor(r, g, b, a)
    r, g, b, a = unpack(ocdb.footer) fra.footer:SetBackdropColor(r, g, b, a)
    r, g, b, a = unpack(ocdb.bindex) fra.index:SetBackdropBorderColor(r, g, b, a)
    r, g, b, a = unpack(ocdb.bfooter) fra.footer:SetBackdropBorderColor(r, g, b, a)
    r, g, b, a = unpack(ocdb.model) fra.model:SetBackdropColor(r, g, b, a)
    r, g, b, a = unpack(ocdb.bmodel) fra.model:SetBackdropBorderColor(r, g, b, a)
end

function OneCompanionOptions:ToggleShowGrip(value)
    local self, fra, ocdb = OneCompanion, OneCompanionFrames, OneCompanion.db.profile.misc
    if value then ocdb.grip = not ocdb.grip end
    if ocdb.grip then fra.grip:Hide() else fra.grip:Show() end
end

function OneCompanionOptions:ToggleShowModelGrip(value)
    local self, fra, ocdb = OneCompanion, OneCompanionFrames, OneCompanion.db.profile.misc
    if value then ocdb.mgrip = not ocdb.mgrip end
    if ocdb.mgrip then fra.mgrip:Hide() else fra.mgrip:Show() end
end

function OneCompanionOptions:ToggleShowTooltips(value)
    local self, fra, g, btn, ocdb = OneCompanion, OneCompanionFrames, GameTooltip, OneCompanionButtons, OneCompanion.db.profile.misc
    if value then ocdb.tip = not ocdb.tip end
    if ocdb.tip then
        fra.cfgButton:SetScript("OnEnter", nil)
        fra.modButton:SetScript("OnEnter", nil)
        for i, j in ipairs(btn) do j:SetScript("OnEnter", nil) end
    else
        fra.cfgButton:SetScript("OnEnter", function(self)
            if GetCVar("UberTooltips") == "1" then
                GameTooltip_SetDefaultAnchor(g, self)
            else
                g:SetOwner(self, "ANCHOR_RIGHT")
            end
            g:AddLine(L["Settings"], 1, 1, 1)
            g:AddLine(L["Click to open/close the Ace3 configuration window"])
            g:Show()
        end)
        fra.modButton:SetScript("OnEnter", function(self)
            if GetCVar("UberTooltips") == "1" then
                GameTooltip_SetDefaultAnchor(g, self)
            else
                g:SetOwner(self, "ANCHOR_RIGHT")
            end
            g:AddLine(L["Companion preview"], 1, 1, 1)
            g:AddLine(L["Click to open/close the 3D preview window"])
            g:AddLine(L["Ctrl-click any companion icon to show its 3D preview"], 0.1843137254901961, 0.8235294117647058, 0)
            g:Show()
        end)
        fra.cfgButton:SetScript("OnLeave", function(self) g:Hide() end)
        fra.modButton:SetScript("OnLeave", function(self) g:Hide() end)
        for i, j in ipairs(btn) do
            j:SetScript("OnEnter", function(self)
                if GetCVar("UberTooltips") == "1" then
                    GameTooltip_SetDefaultAnchor(g, self)
                else
                    g:SetOwner(self, "ANCHOR_RIGHT")
                end
                if g:SetHyperlink("spell:" .. j.spellID) then
                    self.UpdateTooltip = self
                else
                    self.UpdateTooltip = nil
                end
                g:AddLine(L["Ctrl-click to show the 3D preview"], 0.1843137254901961, 0.8235294117647058, 0)
                g:Show()
            end)
        end
    end
end
